package com.etc.zzysys.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.zzsys.entiy.Users;
import com.etc.zzsys.service.UsersService;
import com.etc.zzsys.service.impl.UsersServiceImpl;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/us.do")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UsersService us = new UsersServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UsersServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 设置请求编码
		//request.setCharacterEncoding("utf-8");

		// 设置页面的格式(预期响应的contentType)
		response.setContentType("text/html;charset=utf-8");

		// 通过response的方法得到一个jspwriter
		PrintWriter out = response.getWriter();

		// 获取请求参数
		// 先获取op
		String op = request.getParameter("op");
		if ("del".equals(op)) {
			// 调用del
			doDel(request, response);

		} else if ("update".equals(op)) {
			// 修改用户的操作,先获取各个参数
			String userId = request.getParameter("userId");
			String userName = request.getParameter("userName");
			String userSex = request.getParameter("userSex");
			String userTel = request.getParameter("userTel");
			String userAddress = request.getParameter("userAddress");
			String userLevel = request.getParameter("userLevel");
			String userState = request.getParameter("userState");

			Users u = new Users(Integer.parseInt(userId), userName, userSex, userTel, userAddress,
					Integer.parseInt(userLevel), Integer.parseInt(userState));

			boolean flag = us.updateUsers(u);
			// 遗憾 ：1 没有信息提示 2 因为之前并没有使用mvc模式,所以这里请求不合适
			// request.getRequestDispatcher("users.jsp").forward(request, response);

			// 使用js脚本来实现页面的跳转
			if (flag) {

				out.print("<script>alert('修改成功');location.href='users.jsp'</script>");
			} else {
				out.print("<script>alert('修改失败');location.href='users.jsp'</script>");
			}

			out.close();

		} else if ("login".equals(op)) {
			// 调用doLogin方法
			doLogin(request, response);
		}

	}

	/**
	 * 用户删除
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doDel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 删除操作
		String userId = request.getParameter("userId");
		//
		boolean flag = us.delUsers(Integer.parseInt(userId));

		// 直接跳转到显示界面[因为之前并没有使用mvc模式，所以这里大家可以直接跳转到users.jsp]
		// 遗憾 ：1 没有信息提示 2 因为之前并没有使用mvc模式,所以这里请求不合适
		request.getRequestDispatcher("users.jsp").forward(request, response);
	}

	/**
	 * 登录的方法
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 通过response的方法得到一个jspwriter
		PrintWriter out = response.getWriter();
		// 用户登录
		// 先获取用户名和密码
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");

		System.out.println("userName :"+userName);
		Users u = us.login(userName, userPwd);
		if (u == null) {
			// 用户名或者密码错误
			out.print("<script>alert('登录失败');location.href='back/login.html'</script>");
		} else {
			//1 登录成功，需要将用户登录的信息存储在session中.
			//HttpSession session=request.getSession();
			request.getSession().setAttribute("users", u);
			//将用户登录的信息存储在cookie
			Cookie  cookie =new Cookie("userName",u.getUserName());
			Cookie  cookie1 =new Cookie("pwd",userPwd);
			//使用response.addCookie
			//response.addCookie(cookie);
			//response.addCookie(cookie1);
			out.print("<script>alert('登录成功');location.href='back/mainPage.jsp'</script>");
			
			//使用request的方法进行转发
			//request.getRequestDispatcher("back/mainPage.jsp").forward(request, response);
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
